Skip to content

Introduce expand_string and a new module for logging (mostly for debugging)#40

Merged
MiCurry merged 4 commits into
magnetotellurics:mainfrom
MiCurry:modem_log
Mar 11, 2026
Merged

Introduce expand_string and a new module for logging (mostly for debugging)#40
MiCurry merged 4 commits into
magnetotellurics:mainfrom
MiCurry:modem_log

Conversation

@MiCurry
Copy link
Copy Markdown
Member

@MiCurry MiCurry commented Feb 10, 2026

This pull requests adds in a new utility subroutine, expand_string and a new module, ModEM_logger.

Both of the expand_string subroutine and the ModEM_logger subroutine are taken from NCAR/Los Alamos National Labratory's MPAS-Model: https://github.com/MPAS-Dev/MPAS-Model/blob/master/src/framework/mpas_log.F. MPAS has a BSD licensces and I have included the license in the source code where expand_string and modified MPAS_log_write reside.

Currently, either expand_string or the ModEM_logger are not used anywhere in ModEM (outside of ModEM_logger using expand_string). The expand_string is a very helpful little subroutine that allows a 'printf'-like behavior to formatting strings. There should be numerous usages for such a method.

The ModEM_log is also a helpful function in that it: uses expand_string and individual tasks can open their own logs for writing. This can help wonderfully when debugging MPI issues.

While eventually we could replace all previous write log statements, for now I'd like to include these subroutines for debugging and convenience.

@MiCurry MiCurry requested review from akelbert and dong-hao February 10, 2026 20:41
This commit contains a very helpful little subroutine that can help with
formatting strings. This subroutine was written by Matt Hoffman for the
NCAR's/Los Alamos' MPAS-Model.

It is slightly akin to a printf function:

```Fortran
call expand_string("my string int: $i real: $r", outString, intArgs=(/3/), realArgs=(/42.0/))
```

This function is not used anywhere in ModEM at the moment, but will be helpful
for a future commit that implements a new method for logging; however, this
subroutine should be helpful in various other places, thus I have placed it in
utilities.
This commit adds a new module, ModEM_logger, which contains an easier to use
logging subroutine. There are several notable helpful features of this logger:

1. ModEM_log uses expand_string which was introduced in the last commit, easily
allowing logs to write out specific arguments rather than formatting their own.
2. Using the `ModEM_log_init` subroutine, each individual task can open its own
unique logging file. This can be very helpful when debugging MPI issues.

I have taken the log subroutines from MPAS-Model, and thus included it's
license. I have modified ModEM_log to also take a file unit, thus, instead of
calling ModEM_log_init, one can just call ModEM_log on a file unit already used
by ModEM (say, 0 or 6).

Currently, this module is not used, and I don't have plans to replace any of
ModEM's log statements with it, but I keep added it on various branches for
testing, and thus figured perhaps I should open a pull request to include it
into main.
Copy link
Copy Markdown
Member

@dong-hao dong-hao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MiCurry - looks like a great new feature. Indeed, that would be nice to have for MPI diagnostics - although implementation throughout the repo could involve some extensive works. Do you happen to have a working example to showcase how it works in practice?

@MiCurry
Copy link
Copy Markdown
Member Author

MiCurry commented Feb 25, 2026

@dong-hao I've added some examples to the code comments for the ModEM_log subroutine, but here they are as well:

real :: a, b, c         ! Of course you can also use other real kinds
integer :: int1, int2,
logical :: bool_false, bool_true

! Ensure you call ModEM_log_init() first!
call ModEM_log_init()
    
! Printing several real numbers:
call ModEM_log("Reals: a: $r b: $r c: $r", realArgs=(/a, b, c/)))

! Printing several integer numbers
call ModEM_log("Integers: ($i, $i)", intArgs=(/int1, int2/)))

! Printing logicals
bool_false = .false. 
bool_true = .true.
call ModEM_log("Logical: $l $l", logicArgs=(/bool_false, bool_true/))

! Mix and match as you see fit!
! Note: You do not need to have the arguments (intArgs, logicArgs etc.) in any particular order!
call ModEM_log("Bool: $l - Int: $i - Reals: $r $r $r", intArgs=(/int1/), logicArgs=(/bool_true/), &
                    realArgs=(/a, b, c/))

! Appending Strings
myString = "JOB_NAME"
call ModEM_log("We are in the: '"//trim(myString)//"' function with args: $i, $i", intArgs=(/int1, int2/))

I've been using it quite extensively for the last few weeks, it has some interface quirks that I'd like to work out before including it, but it is very helpful!

Copy link
Copy Markdown
Member

@dong-hao dong-hao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MiCurry for introducing the log utilities。 I drafted a simple unit test code according to your example - I can confirm that the part is running fine as expected. Indeed it is a nice method to have when debugging the parallel code as it provides separate log per task. This is especially useful when one has hundreds of processes to keep track of... The only small issue I found is that the debug remnant (just needs to be commented out).

Comment thread f90/UTILS/ModEM_logger.f90 Outdated
@MiCurry
Copy link
Copy Markdown
Member Author

MiCurry commented Mar 5, 2026

Thanks for giving it a try out @dong-hao. Please feel free to give it another review now that I have addressed your changes.

Copy link
Copy Markdown
Member

@dong-hao dong-hao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MiCurry for the update - I believe this feature will be very useful for future developments.

@MiCurry MiCurry merged commit 225e798 into magnetotellurics:main Mar 11, 2026
1 check passed
@MiCurry MiCurry deleted the modem_log branch March 11, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants